package com.pandas.community.controller;

import com.pandas.community.Utils.CommunityConstant;
import com.pandas.community.Utils.CommunityUtil;
import com.pandas.community.Utils.HostHolder;
import com.pandas.community.entity.Comment;
import com.pandas.community.entity.DiscussPost;
import com.pandas.community.entity.Page;
import com.pandas.community.entity.User;
import com.pandas.community.service.CommentService;
import com.pandas.community.service.DiscussPostService;
import com.pandas.community.service.LikeService;
import com.pandas.community.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping("/discuss")
public class DiscussPostController implements CommunityConstant {
    @Autowired
    private DiscussPostService discussPostService;
    @Autowired
    private HostHolder hostHolder;

    @Autowired
    private UserService userService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private LikeService likeService;

    @RequestMapping(value="/add", method = RequestMethod.POST)
    @ResponseBody
    public String addDiscussPost(String title, String content){
        User user = hostHolder.getUser();
        if(user == null) return CommunityUtil.getJSONString(403,"请登录账户！");

        DiscussPost post = new DiscussPost();
        post.setUserid(user.getId());
        post.setCreateTime(new Date());
        post.setTitle(title);
        post.setContent(content);
        discussPostService.addDiscussPost(post);

        //若发生错误，后面统一处理
        return CommunityUtil.getJSONString(0, "发布帖子成功！");
    }

    //选出帖子
    @RequestMapping(value = "/detail/{discussPostId}",method = RequestMethod.GET)
    public String getDiscussPost(@PathVariable("discussPostId") int discussPostId, Model model, Page page){
        // 帖子
        DiscussPost discussPost = discussPostService.selectDiscussPostById(discussPostId);
        model.addAttribute("discussPost",discussPost);
        // 作者
        User user = userService.selectById(discussPost.getUserid());
        model.addAttribute("postUser",user);

        //帖子点赞数
        long likeCount = likeService.getEntityLikeCount(ENTITY_TYPE_POST,discussPostId);
        //帖子点赞状态
        long likeStatus = hostHolder.getUser() == null ?
                0:likeService.getEntityLikeStatus(hostHolder.getUser().getId(), ENTITY_TYPE_POST, discussPostId);

        model.addAttribute("PostLikeCount", likeCount);
        model.addAttribute("PostLikeStatus", likeStatus);


        // 评论分页信息
        page.setPath("/discuss/detail/" + discussPostId);
        page.setInfoAmount(discussPost.getCommentCount());
        page.setPageCapacity(5);

        // 获得帖子下的所有评论
        // entity-type : 1      entity-id : 某个帖子的id
        List<Comment> commentsOfPost = commentService.selectCommentByEntity(ENTITY_TYPE_POST, discussPost.getId(),
                                        page.getOffset(), page.getPageCapacity());

        List<Map<String, Object>> commentsList = new ArrayList<>();

        // 处理每个评论
        if(commentsOfPost != null){
            for(Comment comment : commentsOfPost){
                Map<String, Object> commentForPost = new HashMap<>();
                commentForPost.put("commentForPost", comment);
                commentForPost.put("commentUser", userService.selectById(comment.getUserid()));

                //评论点赞数
                likeCount = likeService.getEntityLikeCount(ENTITY_TYPE_COMMENT,comment.getId());
                //评论点赞状态
                likeStatus = hostHolder.getUser() == null ?
                        0:likeService.getEntityLikeStatus(hostHolder.getUser().getId(), ENTITY_TYPE_COMMENT, comment.getId());

                commentForPost.put("commentLikeCount", likeCount);
                commentForPost.put("commentLikeStatus", likeStatus);

                // 获得评论的回复——不考虑分页
                // entity-type : 2      entity-id : 某条评论的id
                List<Comment> repliesOfComment = commentService.selectCommentByEntity(ENTITY_TYPE_COMMENT,
                                                comment.getId(), 0, Integer.MAX_VALUE);
                List<Map<String, Object>> repliesList = new ArrayList<>();

                // 处理回复
                if(repliesOfComment != null){
                    for(Comment reply : repliesOfComment){
                        Map<String, Object> replyForComment = new HashMap<>();
                        //回复的内容
                        replyForComment.put("replyForComment", reply);
                        //回复的作者
                        replyForComment.put("replyUser", userService.selectById(reply.getUserid()));
                        // 回复的目标
                        User target = reply.getTargetId() == 0 ? null : userService.selectById(reply.getTargetId());
                        replyForComment.put("target",target);

                        //回复点赞数
                        likeCount = likeService.getEntityLikeCount(ENTITY_TYPE_COMMENT,reply.getId());
                        //回复点赞状态
                        likeStatus = hostHolder.getUser() == null ?
                                0:likeService.getEntityLikeStatus(hostHolder.getUser().getId(), ENTITY_TYPE_COMMENT, reply.getId());

                        replyForComment.put("replyLikeCount", likeCount);
                        replyForComment.put("replyLikeStatus", likeStatus);

                        repliesList.add(replyForComment);
                    }

                }

                commentForPost.put("repliesForComment", repliesList);
                // 添加回复数
                commentForPost.put("amountOfReplies", commentService.selectCommentAmount(ENTITY_TYPE_COMMENT, comment.getId()));

                commentsList.add(commentForPost);
            }

        }
        model.addAttribute("comments", commentsList);


        return "/site/discuss-detail";
    }
}
